<?php namespace App\Models;

use Watson\Validating\ValidatingTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;


class SchoolSpeciality extends Model
{
    use ValidatingTrait;
    use SoftDeletes;

    const CACHE_TAG = 'school_speciality';

    protected $table = 'xyj_schools';

    protected $dates = ['deleted_at'];

    protected $fillable = [
        'name',
        'tid',
        'speciality',
    ];

    protected $rules = [
        'name' => 'required',
        'tid' => 'required|integer',
        // 'speciality' => 'required',
    ];

    protected $validationMessages = [
        'name.required' => '名称必填',
        'tid.required' => '学院id必填',
        'tid.integer' => '学院id必须为整数',
        'speciality.required' => '专业方向必填',

    ];

    /**
     * 用于select的专业列表
     * @return array
     */
    public static function getSpecialityList($deptId)
    {
        $result = \Cache::tags(self::CACHE_TAG)->rememberForever('new-speciality-list:' . $deptId, function () use ($deptId) {
            $infos = self::where('tid', '=', $deptId)->where('level', 3)->select(['id', 'name'])->get()->toArray();
            return json_encode($infos, JSON_UNESCAPED_UNICODE);
        });
        return json_decode($result, true);
    }

    public function clearCache()
    {
        \Cache::tags(School::CACHE_TAG)->flush();
        \Cache::tags(SchoolDept::CACHE_TAG)->flush();
        \Cache::tags(self::CACHE_TAG)->flush();
    }


}